//
//  WPComposeToolBar.m
//  01 微博
//
//  Created by zhoup on 16/5/27.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "WPComposeToolBar.h"

@implementation WPComposeToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}


- (void)setup
{
    // 1.创建5个按钮
    [self setupBtn:@"compose_toolbar_picture" highImage:@"compose_toolbar_picture_highlighted"];
    
    [self setupBtn:@"compose_mentionbutton_background" highImage:@"compose_mentionbutton_background_highlighted"];
    
    [self setupBtn:@"compose_trendbutton_background" highImage:@"compose_trendbutton_background_highlighted"];
    
    [self setupBtn:@"compose_emoticonbutton_background" highImage:@"compose_emoticonbutton_background_highlighted"];
    
    [self setupBtn:@"compose_keyboardbutton_background" highImage:@"compose_keyboardbutton_background_highlighted"];
}
/**
 * 创建一个按钮
 */
- (void)setupBtn:(NSString *)image highImage:(NSString *)highImage
{
    // 1.创建按钮
    UIButton *btn = [[UIButton alloc] init];
    
    // 2.设置按钮默认状态图片
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    // 3.设置按钮高亮状态图片
    [btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    // 4.监听按钮的点击
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    // 5.将按钮添加到view中
    [self addSubview:btn];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 2.调整5个按钮的frame
    NSUInteger count = self.subviews.count;
    CGFloat width = self.width / count;
    for (int i = 0; i < count; i++) {
        UIButton *btn = self.subviews[i];
        btn.height = self.height;
        btn.width = width;
        btn.y = 0;
        btn.x = i * width;
    }
}

@end
